package cn.dglydrpy.study.algorithm.od;

import java.util.*;

/**
 * @author Yyy
 * @date 2024/11/5 12:25
 * @description
 * 题目描述：火星人使用运算符 #、$，给出与地球人公式的等价关系，现有一段火星人的字符串报文，
 * 要求翻译并计算结果。保证输入字符串为合法求值报文，不会出现非法报文和计算溢出。
 * 
 * x#y=4*x+3*y+2
 * x$y=2*x+y+3
 * #优先级高于$
 * 
 * TODO
 */
public class Od0005_MartianCalculation {

    public static void main(String[] args) {
//        String input = "7#6$5#12";

        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()){
            String input = scanner.nextLine();
            System.out.println(answer1(input));
        }
        
        
        
    }

    private static int myAnswer(String input) {
        int ans = 0;
        if(input == null || "".equals(input)){
            return ans;
        }

        char[] chars = input.toCharArray();
        
        // 操作数栈
        LinkedList<Integer> stack = new LinkedList<>();
        List<Character> list = new ArrayList<>();
        
        int opt = 0;
        for(int i = 0;i < chars.length; i++){
            char cur = chars[i];
            // 数字
            if(isNumber(cur)){
                int num = 0;
                while (!isNumber(chars[i])){
                    num = num * 10 +  cur - '0';
                    i++;
                }
                stack.push(num);
                if(i < input.length() && !Character.isDigit(input.charAt(i))){
                    i--;
                }

            }else if('#'== cur){
                i++;
                int num = 0;
                while (i<chars.length && !isNumber(chars[i])){
                    num = num * 10 +  cur - '0';
                    i++;
                }
                if(i < input.length() && !Character.isDigit(input.charAt(i))){
                    i--;
                }
                int num1 = stack.remove(stack.size() -1);
                stack.push(opt1(num1,num));
            }else{
                list.add(cur);
            }

        }

        if (list.size() == 0) {
            return stack.get(0);
            
        }
        
        for (int i = 0; i < list.size(); i++) {
            int num1 = stack.remove(0);
            int num2 = stack.remove(0);
            stack.add(0, opt2(num1,num2));

        }

        return ans;
        
    }
    
    
    private static boolean isNumber(Character c){
        return c-'0'>=0 && '9'-c >= 0;
    }
    
    // x#y=4*x+3*y+2
    private static int opt1(int x,int y){
        return 4*x + 3*y + 2;
    }

    // x$y=2*x+y+3
    private static int opt2(int x,int y){
        return 2*x + y + 3;
    }

    public static Integer answer1(String input) {

        char[] chars = input.toCharArray();
        
//        Stack<Integer> stack = new Stack<>();
        List<Integer> integerList = new ArrayList<>();
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < input.length(); i++) {
            if (Character.isDigit(chars[i])) {
                int num = 0;
                while (i < input.length() && Character.isDigit(chars[i])) {
                    num = num * 10 + chars[i] - '0';
                    i++;
                }
                integerList.add(num);
//                stack.push(num);
                if(i < input.length() && !Character.isDigit(chars[i])){
                    i--;
                }
            } else {
                
                switch (chars[i]){
                    case '#':
                        i++;
                        int num = 0;
                        while (i<chars.length && !isNumber(chars[i])){
                            num = num * 10 +  chars[i] - '0';
                            i++;
                        }
                        if(i < input.length() && !Character.isDigit(chars[i])){
                            i--;
                        }
                        int num1 = integerList.remove(integerList.size() -1);
                        integerList.add(0, 4 * num1 + 3 * num + 2);
                        break;
                    case '$':
                        list.add('$');
                        break;
                }
            }
        }
        if (list.size() == 0) {
            return integerList.get(0);
        }
        for (int i = 0; i < list.size(); i++) {
            int num1 = integerList.remove(0);
            int num2 = integerList.remove(0);
            integerList.add(0,2 * num1 + num2 + 3);
        }
        return integerList.get(0);
    }
}
